package com.yinhe.music.yhmusic.search;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.TabAdapter;
import com.yinhe.music.yhmusic.album.list.AlbumFragment;
import com.yinhe.music.yhmusic.base.BaseFragment;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.music.list.MusicListFragment;
import com.yinhe.music.yhmusic.singer.list.SingerListFragment;
import com.yinhe.music.yhmusic.utils.ViewUtils;
import com.yinhe.music.yhmusic.video.list.MvFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author 何晓华
 * @date 2017/10/25
 */

public class SearchResultFragment extends BaseFragment {
    @BindView(R.id.tabs)
    TabLayout tabs;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    Unbinder unbinder;
    private String key;
    private TabAdapter adapter;
    private String[] title = {"单曲", "歌手", "专辑", "MV", "歌单", "电台", "乐器"};


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_musichall, container, false);
        unbinder = ButterKnife.bind(this, view);
        if (getArguments() != null && TextUtils.isEmpty(key)) {
            key = getArguments().getString("key");
        }

        setViewPager(setFragmentList(key));
        return view;
    }

    private List<Fragment> setFragmentList(String key) {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(MusicListFragment.newInstance(IConstants.SEARCH_MUSIC_LIST, key));
        fragmentList.add(SingerListFragment.newInstance(IConstants.SEARCH_SINGER, key));
        fragmentList.add(AlbumFragment.newInstance(IConstants.SEARCH_ALBUM_LIST, 0, key));
        fragmentList.add(MvFragment.newInstance(IConstants.SEARCH_MV, key));
        fragmentList.add(MenuListFragment.newInstance(key, IConstants.SEARCH_SONG_MENU, null));
        fragmentList.add(RadioListFragment.newInstance(key,IConstants.SEARCH_RADIO_LIST, null));
        fragmentList.add(InstruMentListFragment.newInstance(key));
        return fragmentList;
    }

    public void setData(String key) {
        this.key = key;
        if (adapter != null) {
            adapter.updateFragmentForKey(key);
        }
    }

    private void setViewPager(List<Fragment> fragmentList) {
        tabs.setSelectedTabIndicatorColor(getResources().getColor(R.color.tab_color));
        ViewUtils.reflexIndicator(tabs, 26);
        tabs.setupWithViewPager(viewPager);
        adapter = new TabAdapter(getChildFragmentManager(), fragmentList, title);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(7);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
